package com.huobao.yunying.activitys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.taptwo.android.widget.CircleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;
import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_ketang_;
import com.huobao.yunying.adapters.Adapter_ketang_GridView;
import com.huobao.yunying.adapters.Adapter_keteng_listview;
import com.huobao.yunying.adapters.ImageAdapter_Ketang;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.KetangGrid;
import com.huobao.yunying.entity.KetangGridFather;
import com.huobao.yunying.entity.KetangList;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.views.NoScrollExpandableListView;
import com.huobao.yunying.views.NoScrollGridView;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.yunying.views.ObservableScrollView;
import com.huobao.yunying.views.ObservableScrollView.ScrollViewListener;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
 * 掌婴课堂
 * @author Administrator
 *
 */
public class Act_KeTang extends HuobaoBaseActivity implements OnClickListener,ScrollViewListener,OnGroupClickListener{

	@ViewInject(R.id.scrollview)
	private ObservableScrollView scrollview;
	@ViewInject(R.id.btn_totop)
	private ImageView m_TotopBtn;
	
	
	private int showMoreHght = 0;
	
	
	@ViewInject(R.id.ex_listview)
	private NoScrollExpandableListView listview;
	private Adapter_ketang_ adapter;
	private List<KetangGridFather> list = new ArrayList<KetangGridFather>();
	
	/**
	 * 轮播图相关
	 */
	@ViewInject(R.id.viewflow)
	private ViewFlow m_ViewFlow;
	@ViewInject(R.id.viewflowindic)
	private CircleFlowIndicator m_CircleFlowIndicator;
	private ImageAdapter_Ketang imageadapter;
	@ViewInject(R.id.rtly)
	private RelativeLayout rtly;
	
	@Override
	protected int getContentView() {
		return R.layout.layout_ketang;
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void init() {
		adapter = new Adapter_ketang_(context, list);
		listview.setAdapter(adapter);
		listview.setOnGroupClickListener(this);
		mAbTitleBar.setVisibility(View.GONE);
		m_TotopBtn.setOnClickListener(this);
		scrollview.setScrollViewListener(this);
		getKetang();
	}
	
	private void getKetang(){
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY,"ketang","act","index_type");
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				JSONArray array = jsonObject.getJSONArray("sbanber");
				List<KetangList> spics = JSON.parseArray(array.toString(), KetangList.class);
				
				
				imageadapter = new ImageAdapter_Ketang(context, spics, rtly);
				m_ViewFlow.setAdapter(imageadapter);
				m_ViewFlow.setViewGroup(scrollview);
				m_ViewFlow.setTimeSpan(5000);
				m_ViewFlow.setFlowIndicator(m_CircleFlowIndicator);
				m_ViewFlow.setSideBuffer(spics.size());
				m_ViewFlow.startAutoFlowTimer();
//				ImageUtils.loadimg(imageview, ketangList.getSpic(), new OnImageLoadListener() {
//					
//					@Override
//					public void onLoadSuccess(Drawable drawable) {
//						int newWidth = ScreenUtil.getInstant().getDisWidth(context) ;
//						int newHight = newWidth * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
//						showMoreHght = newHight;
//						LayoutParams params = (LayoutParams) imageview.getLayoutParams();
//						params.width = newWidth;
//						params.height = newHight;
//						imageview.setLayoutParams(params);
//					}
//				});
				JSONArray jsonarray = jsonObject.getJSONArray("skecheng");
				for(int i=0;i<jsonarray.size();i++){
					JSONObject ob = jsonarray.getJSONObject(i);
					KetangGridFather father = JSON.parseObject(ob.toString(), KetangGridFather.class);
					List<KetangGrid> grids = JSON.parseArray(ob.getString("courses"), KetangGrid.class);
					father.setCourses(grids);
					list.add(father);
				}
				adapter.notifyDataSetChanged();
				for (int i = 0; i < adapter.getGroupCount(); i++) {
					listview.expandGroup(i);
				}
				scrollview.smoothScrollTo(0, 0);
			}
			
			@Override
			public void onHttpError(String error) {
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.tv_text:
			Intent intent = new Intent(this, Act_Ketang_More.class);
			startActivity(intent);
			break;
		case R.id.btn_totop:
			scrollview.smoothScrollTo(0, 0);
			break;
		}
	}

//	@Override
//	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//		Intent intent = new Intent(this, Act_Ketang_detail.class);
//		intent.putExtra("more", m_GridAdapter.getList().get(position));
//		startActivity(intent);
//	}

	@Override
	public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
		if(y >= showMoreHght){
			m_TotopBtn.setVisibility(View.VISIBLE);
		}else{
			m_TotopBtn.setVisibility(View.GONE);
		}
	}

@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
	return true;
}

}
